﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgLab.CalendarSyncUtil;
using Google.GData.Calendar;

using MSOffice = Microsoft.Office.Interop;
using System.Collections;

namespace UT
{
    class Program
    {
        static void Main(string[] args)
        {
            //QueryOutlook();
            SyncCalendarBook();
        }

        static void QueryGoogle()
        {
            GoogleCalendarSource source = new GoogleCalendarSource();
            source.Password = "8037873";
            source.User = "Justin1021@gmail.com";
            List<CalendarEntry> listCalendar = source.QueryAllCalendar();
            foreach (CalendarEntry calendar in listCalendar)
            {
                Console.WriteLine("Calendar : " + calendar.Title.Text);
                Console.WriteLine("ID URI: " + calendar.Id.Uri);
                Console.WriteLine("ID : " + GoogleCalendarSource.GetCalendarID( calendar.Id.Uri.Content ) );


                source.Query(calendar, new DateTime(2011, 10, 1), new DateTime(2011, 12, 31));

                foreach (ICalendarItem calItem in source.CalendarItems)
                {
                    Console.WriteLine(calItem.Subject + "|" + calItem.Content + "|" + calItem.Location + "|" + calItem.Start.ToString() + "|" + calItem.End.ToString());
                }
            }
        }


        static void QueryOutlook()
        {
            OutlookCalendarSource source = new OutlookCalendarSource();

            //List<OutlookCalendarBook> allFolders = source.QueryAllCalendar(); 
            ArrayList allFolders = source.QueryAllCalendar();
            foreach (OutlookCalendarBook calendar in allFolders)
            {
                Console.WriteLine("Calendar : " + calendar.Name);

                source.Query(calendar, new DateTime(2011, 11, 24), new DateTime(2011, 11, 25));

                foreach (ICalendarItem calItem in source.CalendarItems)
                {
                    Console.WriteLine(calItem.Subject + "|" + calItem.Content + "|" + calItem.Location + "|" + calItem.Start.ToString() + "|" + calItem.End.ToString());
                }
            }
        }

        static void SyncCalendarBook()
        {
            DateTime syncStart = new DateTime(2011, 12, 1);
            DateTime syncEnd = new DateTime(2012, 1, 1);

            GoogleCalendarSource sourceGoogle = new GoogleCalendarSource();
            sourceGoogle.Password = "8037873";
            sourceGoogle.User = "Justin1021@gmail.com";

            CalendarEntry calGoogle = sourceGoogle.QueryCalendar("Polaris");
            if (calGoogle == null)
            {
                Console.WriteLine("Can not load Google Calendar:Polaris");
                return;
            }

            GoogleCalendarBook calbookGoogle = new GoogleCalendarBook(calGoogle, sourceGoogle);

            sourceGoogle.Query(calGoogle, syncStart, syncEnd);

            // Outlook
            OutlookCalendarSource sourceOutlook = new OutlookCalendarSource();
            ArrayList allFolders = sourceOutlook.QueryAllCalendar();
            if (allFolders.Count <= 0)
            {
                Console.WriteLine("Can not load Outlook Calendar");
                return;
            }

            OutlookCalendarBook calbookOutlook = (OutlookCalendarBook)allFolders[0];

            sourceOutlook.Query(calbookOutlook, syncStart, syncEnd);

            CalendarSyncHelper syncHelper = new CalendarSyncHelper();
            syncHelper.Source = sourceOutlook.CalendarItems;
            syncHelper.Destination = sourceGoogle.CalendarItems;

            syncHelper.Sync(calbookGoogle);
        }
    }
}
